package com.fzrskj.cloud.free.user.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.user.bean.UserSecGroupHost;
import com.fzrskj.cloud.free.user.bean.UserSecGroupHostView;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserSecGroupHostDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public UserSecGroupHostDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	//to add new record to database by bean
	public int addUserSecGroupHost(UserSecGroupHost obj){
		String sql = "insert into user_secgroup_host(id,sgId,instanceId,userId,userName)"+
			"values(?,?,?,?,?);";
		Object[] prm={obj.getId(),obj.getSgId(),obj.getInstanceId(),obj.getUserId(),obj.getUserName()};
		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to add new record to database by map
	public int addUserSecGroupHost(Map<String, Object> values){
		int ret = db.insertRecord("user_secgroup_host",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delUserSecGroupHost(int id){
		String sql="delete from user_secgroup_host where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delUserSeGroupHost(Map<String, Object> cdt){
		int ret = db.deleteRecord("user_secgroup_host",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modUserSecGroupHost(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("user_secgroup_host", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	/**
	 * 查询安全组关联实例列表(分页)
	 * @param cdt
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<UserSecGroupHostView> queryUsgHostList(Map<String, Object> cdt, int page, int pagesize){
		StringBuilder sb=new StringBuilder("select ush.*,uo.instanceName,uo.regionName,uo.zoneName,uo.publicIP,uo.privateIP from user_secgroup_host ush,user_openstack uo ");
		sb.append("where ush.instanceId=uo.instanceId ");

		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}
		if ((page>0) && (pagesize>0)) {
			sb.append(String.format(" limit %d,%d",(page-1)*pagesize,pagesize));
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<UserSecGroupHostView> ret = db.queryForBeans(sb.toString(), UserSecGroupHostView.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}

	/**
	 * 统计安全组中实例
	 * @param cdt
	 * @return
	 */
	public int queryUsgHostCount(Map<String, Object> cdt){
		List<Object> list = new ArrayList<Object>();
		StringBuilder sb=new StringBuilder("select count(*) from user_secgroup_host ush,user_openstack uo ");
		sb.append("where ush.instanceId=uo.instanceId ");

		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}

		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}

		Long ret = (Long) db.queryFieldValue(sb.toString(), Long.class, params, 1);
		this.setErrCode(db.getErrCode());
		return ret.intValue();
	}

	/**
	 * 查询安全组关联实例列表
	 * @param cdt
	 * @return
	 */
	public List<UserSecGroupHost> queryUserSecGroupHost(Map<String, Object> cdt){
		StringBuilder sb=new StringBuilder("select * from user_secgroup_host where 1=1 ");	
		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		} 
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<UserSecGroupHost> ret = db.queryForBeans(sb.toString(), UserSecGroupHost.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}
}
